import store from "@/store"
import {getLoginUrl} from "@/modules/util"
// console.log(store)

const middlewares = {
  //检测是否登录
  requiresAuth({ to, from, next }, value) {
    if (!store.state.user.sign_in) {
      if(value===true) {
        history.go(-1)
      }else if(value === 'toLoginPage') {
        location.href = getLoginUrl()
      }
      else {
        next({ name: value })
      }
    }
  },
  //检查是否有选择了鱼id
  requiresFish({ to, from, next }) {
    if ( !store.state.posts.supply.kind.id ) {
      next({ name: "suppliesBoardFishes" })
    }
  },
  requiresFishDemand({ to, from, next }) {
    if ( !store.state.posts.demand.kind.id ) {
      next({ name: "demandsBoardFishes" })
    }
  },
  showTab({ to, from, next }, value) {
    store.commit('showTab')
  },
  tabIndex({ to, from, next }, value) {
    store.commit('setTabIndex', value)
  }
}

export default middlewares
